<template>
  <div class="sql-node-card" ref="card">
    <!-- 删除蓝点锚点相关代码 -->
    <div class="header">
      <span class="icon"></span>
      <span class="title">SQL查询节点</span>
    </div>
    <div class="desc">执行SQL查询获取数据库数据</div>
    <div class="section-title">数据库配置</div>
    <div class="config-list">
      <div class="config-row">
        <label>数据源：</label>
        <el-select v-model="localConfig.dataSource" size="small" style="width: 100%;" placeholder="请选择数据源" @change="emitChange">
          <el-option label="主数据库" value="main" />
          <el-option label="从数据库" value="slave" />
          <el-option label="报表数据库" value="report" />
        </el-select>
      </div>
      <div class="config-row">
        <label>查询类型：</label>
        <el-select v-model="localConfig.queryType" size="small" style="width: 100%;" @change="emitChange">
          <el-option label="SELECT" value="select" />
          <el-option label="INSERT" value="insert" />
          <el-option label="UPDATE" value="update" />
          <el-option label="DELETE" value="delete" />
        </el-select>
      </div>
      <div class="config-row">
        <label>超时时间：</label>
        <el-input-number v-model="localConfig.timeout" :min="1000" :max="60000" :step="1000" size="small" style="width: 100%;" @change="emitChange" />
        <span style="margin-left: 8px; color: #888;">毫秒</span>
      </div>
    </div>
    <div class="section-title">SQL语句</div>
    <div class="sql-area">
      <div class="sql-header">
        <span>SQL</span>
        <el-button size="mini" type="primary" @click="formatSQL">格式化</el-button>
      </div>
      <el-input 
        v-model="localConfig.sql" 
        type="textarea" 
        :rows="4"
        placeholder="请输入SQL语句，可使用 {变量名} 引用输入参数"
        class="sql-input"
        @input="emitChange"
      />
    </div>
    <div class="section-title">参数绑定</div>
    <div class="params-area">
      <div v-for="(param, idx) in localConfig.parameters" :key="idx" class="param-row">
        <el-input v-model="param.name" placeholder="参数名" size="small" style="width: 30%; margin-right: 8px;" @input="emitChange" />
        <el-input v-model="param.value" placeholder="参数值" size="small" style="width: 40%; margin-right: 8px;" @input="emitChange" />
        <el-select v-model="param.type" size="small" style="width: 20%; margin-right: 8px;" @change="emitChange">
          <el-option label="String" value="string" />
          <el-option label="Number" value="number" />
          <el-option label="Date" value="date" />
        </el-select>
        <el-button size="mini" type="danger" @click="removeParam(idx)">删除</el-button>
      </div>
      <el-button size="mini" type="primary" @click="addParam" style="margin-top: 8px;">添加参数</el-button>
    </div>
    <div class="section-title">输出配置</div>
    <div class="config-list">
      <div class="config-row">
        <el-checkbox v-model="localConfig.saveToVariable" @change="emitChange">保存到变量</el-checkbox>
      </div>
      <div class="config-row" v-if="localConfig.saveToVariable">
        <label>变量名：</label>
        <el-input v-model="localConfig.outputVariable" size="small" placeholder="请输入输出变量名" @input="emitChange" />
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'SQLQueryNodeCard',
  props: {
    config: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      localConfig: { ...this.config }
    }
  },
  watch: {
    config: {
      handler(newVal) {
        this.localConfig = { ...newVal }
      },
      deep: true
    }
  },
  methods: {
    emitChange() {
      this.$emit('change', { ...this.localConfig })
    },
    addParam() {
      this.localConfig.parameters.push({ name: '', value: '', type: 'string' });
      this.emitChange();
    },
    removeParam(idx) {
      this.localConfig.parameters.splice(idx, 1);
      this.emitChange();
    },
    formatSQL() {
      // 简单的SQL格式化，实际项目中可以使用更专业的格式化库
      try {
        // 这里可以集成 sql-formatter 或其他SQL格式化工具
        this.$message.success('SQL格式化完成');
      } catch (error) {
        this.$message.error('SQL格式化失败');
      }
    }
  }
};
</script>

<style scoped>
.sql-node-card {
  width: 300px;
  background: #fff;
  border: 1px solid #e5e7eb;
  border-radius: 10px;
  box-shadow: 0 2px 8px 0 rgba(0,0,0,0.04);
  padding: 16px;
  position: relative;
}

.node-dot {
  position: absolute;
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: #2979ff;
  border: 2px solid #fff;
  box-shadow: 0 0 2px #2979ff;
  cursor: pointer;
  z-index: 10;
  transition: box-shadow 0.2s;
}

.node-dot--left-top {
  top: -6px;
  left: -6px;
}

.node-dot--right-top {
  top: -6px;
  right: -6px;
}

.node-dot:hover {
  box-shadow: 0 0 8px #2979ff;
}

.header {
  display: flex;
  align-items: center;
  margin-bottom: 8px;
  position: relative;
}

.icon {
  width: 32px;
  height: 32px;
  background: #e0f2f1;
  border-radius: 50%;
  display: inline-block;
  margin-right: 8px;
  position: relative;
}

.icon::after {
  content: '🗄️';
  display: block;
  width: 16px;
  height: 16px;
  font-size: 16px;
  position: absolute;
  left: 8px;
  top: 8px;
}

.title {
  font-size: 18px;
  font-weight: bold;
}

.desc {
  color: #888;
  font-size: 13px;
  margin-bottom: 8px;
}

.section-title {
  font-weight: bold;
  margin-bottom: 8px;
  margin-top: 12px;
}

.section-title:first-of-type {
  margin-top: 0;
}

.config-list {
  background: #fafbfc;
  border-radius: 6px;
  padding: 12px;
}

.config-row {
  margin-bottom: 12px;
  display: flex;
  align-items: center;
}

.config-row:last-child {
  margin-bottom: 0;
}

.config-row label {
  display: block;
  margin-bottom: 4px;
  font-size: 14px;
  color: #333;
  width: 100%;
}

.sql-area {
  background: #fafbfc;
  border-radius: 6px;
  padding: 12px;
}

.sql-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
  font-weight: bold;
  color: #333;
}

.sql-input {
  font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
  font-size: 12px;
}

.params-area {
  background: #fafbfc;
  border-radius: 6px;
  padding: 12px;
}

.param-row {
  display: flex;
  align-items: center;
  margin-bottom: 8px;
}
</style> 